package com.singing.util;

import java.sql.Connection;

public class DbUtil {
    public static final String DB_DRIVER = "com.mysql.cj.jdbc.Driver";
    public static final String DB_URL = "jdbc:mysql://localhost:3306/singing?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    public static final String DB_USER = "root";
    public static final String DB_PASSWORD = "root";

    //获取连接
    public static Connection getConnection() {
        try {
            Class.forName(DB_DRIVER);
            Connection connection = java.sql.DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
            return connection;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取连接失败");
        }
    }


    //关闭连接
    public static void closeResource(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


    //关闭资源
    public static void closeResource(Connection connection, java.sql.Statement statement) {
        closeResource(connection);
        if (statement != null) {
            try {
                statement.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    //关闭资源
    public static void closeResource(Connection connection, java.sql.Statement statement, java.sql.ResultSet resultSet) {
        closeResource(connection, statement);
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
